package top.yokey.android.activity;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import top.yokey.android.R;
import top.yokey.android.base.App;
import top.yokey.android.base.BaseActivity;
import top.yokey.android.http.BaseCallBack;
import top.yokey.android.http.HttpHelp;

public class ModifyGenderActivity extends BaseActivity {


    private AppCompatImageView backImageView;
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private AppCompatTextView modifyTextView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_modify_gender);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {

        backImageView = findViewById(R.id.backImageView);
        radioButton1 = findViewById(R.id.radioButton1);
        radioButton2 = findViewById(R.id.radioButton2);
        modifyTextView = findViewById(R.id.modifyTextView);

        if (App.get().getMemberBean().getMemberGender() == 1) {
            radioButton1.setChecked(true);
            radioButton2.setChecked(false);
        } else {
            radioButton1.setChecked(false);
            radioButton2.setChecked(true);
        }

    }

    @Override
    public void initEvent() {

        backImageView.setOnClickListener(view -> finish());

        modifyTextView.setOnClickListener(view -> {
            String gender = radioButton1.isChecked() ? "1" : "2";
            modifyTextView.setEnabled(false);
            modifyTextView.setText("正在修改性别...");
            HttpHelp.get().ready("member", "modifyGender")
                    .add("new_gender", gender)
                    .get(new BaseCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            App.get().getMemberBean().setMemberGender(radioButton1.isChecked() ? 1 : 2);
                            Toast.makeText(ModifyGenderActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }

                        @Override
                        public void onError(String reason) {
                            Toast.makeText(ModifyGenderActivity.this, reason, Toast.LENGTH_SHORT).show();
                            modifyTextView.setEnabled(true);
                            modifyTextView.setText("修改性别");
                        }
                    });

        });

    }

}
